package com.bilibili.juc.cf.one;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

public class CompleteFutureDemoTwo {

    CompletableFuture<Integer> cf1 = new CompletableFuture<>();

    public CompletableFuture<Integer> processMethod01(){
        System.out.println("==>> CompleteFutureDemoTwo.processMethod01... start");
        /*
         return cf1.thenCompose(number -> {
            CompletableFuture<Integer> future = new CompletableFuture<>();
            future.complete(number+10);
            return future;
        });
        * */
        //这是会等待cf1完成后再执行 thenCompose 后面的操作
        return cf1.thenCompose(new Function<Integer, CompletionStage<Integer>>() {
            @Override
            public CompletionStage<Integer> apply(Integer number) {
                CompletableFuture<Integer> future = new CompletableFuture<>();
                System.out.println("==>> CompleteFutureDemoTwo.processMethod01... end " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
                future.complete(number+10);
                return future;
            }
        });
    }

    public void finishCf1(){
        System.out.println("***= CompleteFutureDemoTwo.finishCf1... start "+ DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        cf1.complete(10);
        System.out.println("***= CompleteFutureDemoTwo.finishCf1... end "+ DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
    }

    public static void main(String[] args) throws Exception{
        CompleteFutureDemoTwo completeFutureDemoTwo = new CompleteFutureDemoTwo();
        Thread t1 = new Thread(()->{
            System.out.println("@@ t1 start..." + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            CompletableFuture<Integer> future = completeFutureDemoTwo.processMethod01();
            future.thenAccept(rs -> System.out.println("****** 计算的结果=> " + rs + " ," + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)));
            System.out.println("@@ t1 end..." + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });
        // + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)
        Thread t2 = new Thread(()->{
            System.out.println("## t2 start..." + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            completeFutureDemoTwo.finishCf1();
            System.out.println("## t2 end..." + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });

        t1.start();
        TimeUnit.SECONDS.sleep(1);
        t2.start();
        System.out.println("*** main task *** " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        System.in.read();
    }
    /*
@@ t1 start...2025-01-09 09:17:30
==>> CompleteFutureDemoTwo.processMethod01... start
@@ t1 end...2025-01-09 09:17:30
*** main task *** 2025-01-09 09:17:31
## t2 start...2025-01-09 09:17:31
***= CompleteFutureDemoTwo.finishCf1... start 2025-01-09 09:17:33
==>> CompleteFutureDemoTwo.processMethod01... end 2025-01-09 09:17:36
****** 计算的结果=> 20 ,2025-01-09 09:17:36
***= CompleteFutureDemoTwo.finishCf1... end 2025-01-09 09:17:36
## t2 end...2025-01-09 09:17:36
     */


}
